package cn.s1995.sloth.app.controller;

import java.util.Objects;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.entity.ProdEvaluate;
import cn.s1995.sloth.bizs.oo.req.OrderEvaluateREQ;
import cn.s1995.sloth.bizs.oo.req.ProdEvaluateREQ;
import cn.s1995.sloth.bizs.oo.vo.ProdEvaluateVO;
import cn.s1995.sloth.bizs.service.ProdEvaluateService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

/**
 * 产品评价
 *
 * @author sloth
 * @date 2024-01-02 15:35:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/m/prodEvaluate")
@Tag(description = "ProdEvaluate", name = "APP-产品评价")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiProdEvaluateController {

    private final ProdEvaluateService prodEvaluateService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 产品评价
     * @return
     */
    @Inner(false)
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取评价分页", description = "获取评价分页")
    @GetMapping("/page")
    public R<Page<ProdEvaluate>> getPage(@ParameterObject Page page, @ParameterObject ProdEvaluate req) {
        LambdaQueryWrapper<ProdEvaluate> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(req.getProdId()), ProdEvaluate::getProdId, req.getProdId());
		wrapper.eq(ProdEvaluate::getState, BizCst.CommonAuditState.approve);
		wrapper.orderByDesc(ProdEvaluate::getCreateTime);
        return R.ok(prodEvaluateService.page(page, wrapper));
    }

	@ApiOperationSupport(order = 2)
    @Operation(summary = "获取产品的评价", description = "获取产品的评价")
    @GetMapping("/prod/{id}")
	@Parameters({
	    @Parameter(name = "id",description = "id", required = true, in = ParameterIn.PATH),
	})
    public R<ProdEvaluateVO> getByProdId(@PathVariable("id") Long id) {
		return R.ok(prodEvaluateService.getProdData(id));
    }

	@ApiOperationSupport(order = 3)
    @Operation(summary = "获取单条评价详情", description = "获取单条评价详情")
    @GetMapping("/detail/{id}")
	@Parameters({
	    @Parameter(name = "id",description = "id", required = true, in = ParameterIn.PATH),
	})
    public R<ProdEvaluate> detail(@PathVariable("id") Long id) {
		return R.ok(prodEvaluateService.getById(id));
    }

    /**
     * 批量新增评价
     * @param req 产品评价
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "评价订单（批量评价产品）", description = "评价订单（批量评价产品）")
    @PostMapping("/addBatch")
    public R addBatch(@Valid @RequestBody OrderEvaluateREQ req) {
		return prodEvaluateService.addBatch(req);
    }

    /**
     * 修改产品评价
     * @param req 产品评价
     * @return R
     */
	@ApiOperationSupport(order = 15)
    @Operation(summary = "修改评价", description = "修改评价")
    @PutMapping("/update")
    public R update(@Valid @RequestBody ProdEvaluateREQ req) {
		return prodEvaluateService.saveProdEvaluate(req);
    }

//    /**
//     * 修改产品评价
//     * @param req 产品评价
//     * @return R
//     */
//	@ApiOperationSupport(order = 20)
//    @Operation(summary = "修改产品评价", description = "修改产品评价")
//    @SysLog("修改产品评价")
//    @PutMapping
//    public R updateById(@RequestBody ProdEvaluate req) {
//        return R.ok(prodEvaluateService.updateById(req));
//    }

//    /**
//     * 通过id删除产品评价
//     * @param ids id列表
//     * @return R
//     */
//	@ApiOperationSupport(order = 30)
//    @Operation(summary = "通过id删除产品评价", description = "通过id删除产品评价")
//    @SysLog("通过id删除产品评价")
//    @DeleteMapping
//    public R remove(@RequestBody Long[] ids) {
//        return R.ok(prodEvaluateService.removeBatchByIds(CollUtil.toList(ids)));
//    }
}
